What is antlr4?
The antlr4 npm package is a powerful tool for building language parsers. It allows developers to define grammars for programming languages, data formats, and more, and then generate parsers that can process text according to those grammars.
What are antlr4's main functionalities?
Grammar Definition
This code demonstrates how to define a grammar and use it to parse an input string. The grammar is defined in separate files (MyGrammarLexer and MyGrammarParser), and the input string is processed according to the rules defined in the grammar.
const antlr4 = require('antlr4');
const { ANTLRInputStream, CommonTokenStream } = antlr4;
const MyGrammarLexer = require('./MyGrammarLexer');
const MyGrammarParser = require('./MyGrammarParser');
const input = 'your input string';
const inputStream = new ANTLRInputStream(input);
const lexer = new MyGrammarLexer(inputStream);
const tokenStream = new CommonTokenStream(lexer);
const parser = new MyGrammarParser(tokenStream);
const tree = parser.startRule();
Tree Walking
This code demonstrates how to walk through the parse tree generated by the parser. A listener (MyGrammarListener) is used to respond to events as the tree is walked, allowing for custom processing of the parsed input.
const antlr4 = require('antlr4');
const { ParseTreeWalker } = antlr4.tree;
const MyGrammarListener = require('./MyGrammarListener');
const input = 'your input string';
const inputStream = new ANTLRInputStream(input);
const lexer = new MyGrammarLexer(inputStream);
const tokenStream = new CommonTokenStream(lexer);
const parser = new MyGrammarParser(tokenStream);
const tree = parser.startRule();
const listener = new MyGrammarListener();
ParseTreeWalker.DEFAULT.walk(listener, tree);
Custom Visitor
This code demonstrates how to use a custom visitor (MyGrammarVisitor) to traverse the parse tree. The visitor pattern allows for more flexible and reusable tree processing compared to listeners.
const antlr4 = require('antlr4');
const MyGrammarVisitor = require('./MyGrammarVisitor');
const input = 'your input string';
const inputStream = new ANTLRInputStream(input);
const lexer = new MyGrammarLexer(inputStream);
const tokenStream = new CommonTokenStream(lexer);
const parser = new MyGrammarParser(tokenStream);
const tree = parser.startRule();
const visitor = new MyGrammarVisitor();
const result = visitor.visit(tree);
Other packages similar to antlr4
pegjs
PEG.js is a simple parser generator for JavaScript based on the parsing expression grammar formalism. It is similar to ANTLR in that it allows you to define grammars and generate parsers, but it is generally considered to be easier to use for simpler grammars and smaller projects.
nearley
Nearley is a fast, powerful parser toolkit for JavaScript. It is similar to ANTLR in that it allows you to define grammars and generate parsers, but it uses a different parsing algorithm (Earley parsing) which can handle a wider range of grammars, including ambiguous ones.
jison
Jison is a parser generator for JavaScript that is similar to ANTLR. It allows you to define grammars using a syntax similar to Bison/Yacc and generates parsers that can be used in JavaScript applications. Jison is often used for building compilers and interpreters for custom languages.
JavaScript target for ANTLR 4
JavaScript runtime libraries for ANTLR 4
This runtime is available through npm. The package name is 'antlr4'.
This runtime has been tested in Node.js, Safari, Firefox, Chrome and IE.
See www.antlr.org for more information on ANTLR
See Javascript Target
for more information on using ANTLR in JavaScript
This runtime requires node version >= 16.
ANTLR 4 runtime is available in 10 target languages, and favors consistency of versioning across targets.
As such it cannot follow recommended NPM semantic versioning.
If you install a specific version of antlr4, we strongly recommend you remove the corresponding ^ in your package.json.